package org.javaforever.gatescore.shiroauth.vue;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.JsGenerator;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.StatementList;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.shiroauth.verb.LoginUser;
import org.javaforever.gatescore.utils.StringUtil;

public class UsersStoreJSGenerator extends JsGenerator{
	protected FrontDomain domain;
	
	public UsersStoreJSGenerator(FrontDomain domain) {
		super();
		this.domain = domain;
		this.standardName = "UserJS";
		this.fileName = "user.js";
	}
	
	@Override
	public StatementList generateStatementList() {
		List<Writeable> sList = new ArrayList<Writeable>();
		LoginUser loginUser = new LoginUser(this.domain);
;		sList.add(new Statement(1000L,0,"import { "+StringUtil.lowerFirst(loginUser.getVerbName())+", logout, getUserInfo } from '@/api/login'"));
		sList.add(new Statement(2000L,0,"import { getToken, setToken, removeToken } from '@/utils/auth'"));
		sList.add(new Statement(3000L,0,""));
		sList.add(new Statement(4000L,0,"const user = {"));
		sList.add(new Statement(5000L,1,"state: {"));
		sList.add(new Statement(6000L,2,"user: '',"));
		sList.add(new Statement(7000L,2,"status: '',"));
		sList.add(new Statement(8000L,2,"code: '',"));
		sList.add(new Statement(9000L,2,"token: getToken(),"));
		sList.add(new Statement(10000L,2,"name: '',"));
		sList.add(new Statement(11000L,2,"avatar: '',"));
		sList.add(new Statement(12000L,2,"introduction: '',"));
		sList.add(new Statement(13000L,2,"roles: [],"));
		sList.add(new Statement(13500L,2,"perms: [],"));
		sList.add(new Statement(14000L,2,"setting: {"));
		sList.add(new Statement(15000L,3,"articlePlatform: []"));
		sList.add(new Statement(16000L,2,"}"));
		sList.add(new Statement(17000L,1,"},"));
		sList.add(new Statement(18000L,0,""));
		sList.add(new Statement(19000L,1,"mutations: {"));
		sList.add(new Statement(20000L,2,"SET_CODE: (state, code) => {"));
		sList.add(new Statement(21000L,3,"state.code = code"));
		sList.add(new Statement(22000L,2,"},"));
		sList.add(new Statement(23000L,2,"SET_TOKEN: (state, token) => {"));
		sList.add(new Statement(24000L,3,"state.token = token"));
		sList.add(new Statement(25000L,2,"},"));
		sList.add(new Statement(26000L,2,"SET_INTRODUCTION: (state, introduction) => {"));
		sList.add(new Statement(27000L,3,"state.introduction = introduction"));
		sList.add(new Statement(28000L,2,"},"));
		sList.add(new Statement(29000L,2,"SET_SETTING: (state, setting) => {"));
		sList.add(new Statement(30000L,3,"state.setting = setting"));
		sList.add(new Statement(31000L,2,"},"));
		sList.add(new Statement(32000L,2,"SET_STATUS: (state, status) => {"));
		sList.add(new Statement(33000L,3,"state.status = status"));
		sList.add(new Statement(34000L,2,"},"));
		sList.add(new Statement(35000L,2,"SET_NAME: (state, name) => {"));
		sList.add(new Statement(36000L,3,"state.name = name"));
		sList.add(new Statement(37000L,2,"},"));
		sList.add(new Statement(38000L,2,"SET_AVATAR: (state, avatar) => {"));
		sList.add(new Statement(39000L,3,"state.avatar = avatar"));
		sList.add(new Statement(40000L,2,"},"));
		sList.add(new Statement(41000L,2,"SET_ROLES: (state, roles) => {"));
		sList.add(new Statement(42000L,3,"state.roles = roles"));
		sList.add(new Statement(43000L,2,"},"));
		
		sList.add(new Statement(43100L,2,"SET_PERMS: (state, perms) => {"));
		sList.add(new Statement(43200L,3,"state.perms = perms"));
		sList.add(new Statement(43300L,2,"}"));
		
		sList.add(new Statement(44000L,1,"},"));
		sList.add(new Statement(45000L,0,""));
		sList.add(new Statement(46000L,1,"actions: {"));
		sList.add(new Statement(47000L,2,"// 用户名登录"));
		sList.add(new Statement(48000L,2,"LoginByUsername({ commit }, userInfo) {"));
		sList.add(new Statement(49000L,3,"const username = userInfo.username.trim()"));
		sList.add(new Statement(50000L,3,"return new Promise((resolve, reject) => {"));
		sList.add(new Statement(51000L,4,StringUtil.lowerFirst(loginUser.getVerbName())+"(username, userInfo.password).then(response => {"));
		sList.add(new Statement(52000L,5,"const data = response.data"));
		sList.add(new Statement(53000L,5,"commit('SET_TOKEN', data.token)"));
		sList.add(new Statement(54000L,5,"setToken(response.data.token)"));
		sList.add(new Statement(55000L,5,"resolve(data)"));
		sList.add(new Statement(56000L,4,"}).catch(error => {"));
		sList.add(new Statement(57000L,5,"reject(error)"));
		sList.add(new Statement(58000L,4,"})"));
		sList.add(new Statement(59000L,3,"})"));
		sList.add(new Statement(60000L,2,"},"));
		sList.add(new Statement(61000L,0,""));
		sList.add(new Statement(62000L,2,"// 获取用户信息"));
		sList.add(new Statement(63000L,2,"GetUserInfo({ commit, state }) {"));
		sList.add(new Statement(64000L,3,"return new Promise((resolve, reject) => {"));
		sList.add(new Statement(65000L,4,"getUserInfo(state.token).then(response => {"));
		sList.add(new Statement(66000L,5,"// 由于mockjs 不支持自定义状态码只能这样hack"));
		sList.add(new Statement(67000L,5,"if (!response.data) {"));
		sList.add(new Statement(68000L,6,"reject('Verification failed, please login again.')"));
		sList.add(new Statement(69000L,5,"}"));
		sList.add(new Statement(70000L,5,"const data = response.data"));
		sList.add(new Statement(71000L,0,""));
		sList.add(new Statement(72000L,5,"if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组"));
		sList.add(new Statement(73000L,6,"commit('SET_ROLES', data.roles)"));
		sList.add(new Statement(73500L,6,"commit('SET_PERMS', data.perms)"));
		sList.add(new Statement(74000L,5,"} else {"));
		sList.add(new Statement(75000L,6,"reject('getInfo: roles must be a non-null array!')"));
		sList.add(new Statement(76000L,5,"}"));
		sList.add(new Statement(77000L,0,""));
		sList.add(new Statement(78000L,5,"commit('SET_NAME', data.user."+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+")"));
		sList.add(new Statement(79000L,5,"commit('SET_AVATAR', '../static/images/rocket.png')"));
		sList.add(new Statement(80000L,5,"// commit('SET_INTRODUCTION', data.introduction)"));
		sList.add(new Statement(81000L,5,"resolve(response)"));
		sList.add(new Statement(82000L,4,"}).catch(error => {"));
		sList.add(new Statement(83000L,5,"reject(error)"));
		sList.add(new Statement(84000L,4,"})"));
		sList.add(new Statement(85000L,3,"})"));
		sList.add(new Statement(86000L,2,"},"));
		sList.add(new Statement(87000L,0,""));
		sList.add(new Statement(88000L,2,"// 第三方验证登录"));
		sList.add(new Statement(89000L,2,"// LoginByThirdparty({ commit, state }, code) {"));
		sList.add(new Statement(90000L,2,"//   return new Promise((resolve, reject) => {"));
		sList.add(new Statement(91000L,2,"//     commit('SET_CODE', code)"));
		sList.add(new Statement(92000L,2,"//     loginByThirdparty(state.status, state.email, state.code).then(response => {"));
		sList.add(new Statement(93000L,2,"//       commit('SET_TOKEN', response.data.token)"));
		sList.add(new Statement(94000L,2,"//       setToken(response.data.token)"));
		sList.add(new Statement(95000L,2,"//       resolve()"));
		sList.add(new Statement(96000L,2,"//     }).catch(error => {"));
		sList.add(new Statement(97000L,2,"//       reject(error)"));
		sList.add(new Statement(98000L,2,"//     })"));
		sList.add(new Statement(99000L,2,"//   })"));
		sList.add(new Statement(100000L,2,"// },"));
		sList.add(new Statement(101000L,0,""));
		sList.add(new Statement(102000L,2,"// 登出"));
		sList.add(new Statement(103000L,2,"LogOut({ commit, state }) {"));
		sList.add(new Statement(104000L,3,"return new Promise((resolve, reject) => {"));
		sList.add(new Statement(105000L,4,"logout(state.token).then(() => {"));
		sList.add(new Statement(106000L,5,"commit('SET_TOKEN', '')"));
		sList.add(new Statement(107000L,5,"commit('SET_ROLES', [])"));
		sList.add(new Statement(107500L,5,"commit('SET_PERMS', [])"));
		sList.add(new Statement(108000L,5,"removeToken()"));
		sList.add(new Statement(109000L,5,"resolve()"));
		sList.add(new Statement(110000L,4,"}).catch(error => {"));
		sList.add(new Statement(111000L,5,"reject(error)"));
		sList.add(new Statement(112000L,4,"})"));
		sList.add(new Statement(113000L,3,"})"));
		sList.add(new Statement(114000L,2,"},"));
		sList.add(new Statement(115000L,0,""));
		sList.add(new Statement(116000L,2,"// 前端 登出"));
		sList.add(new Statement(117000L,2,"FedLogOut({ commit }) {"));
		sList.add(new Statement(118000L,3,"return new Promise(resolve => {"));
		sList.add(new Statement(119000L,4,"commit('SET_TOKEN', '')"));
		sList.add(new Statement(120000L,4,"removeToken()"));
		sList.add(new Statement(121000L,4,"resolve()"));
		sList.add(new Statement(122000L,3,"})"));
		sList.add(new Statement(123000L,2,"},"));
		sList.add(new Statement(124000L,0,""));
		sList.add(new Statement(125000L,2,"// 动态修改权限"));
		sList.add(new Statement(126000L,2,"ChangeRoles({ commit, dispatch }, role) {"));
		sList.add(new Statement(127000L,3,"return new Promise(resolve => {"));
		sList.add(new Statement(128000L,4,"commit('SET_TOKEN', role)"));
		sList.add(new Statement(129000L,4,"setToken(role)"));
		sList.add(new Statement(130000L,4,"getUserInfo(role).then(response => {"));
		sList.add(new Statement(131000L,5,"const data = response.data"));
		sList.add(new Statement(132000L,5,"commit('SET_ROLES', data.roles)"));
		sList.add(new Statement(132500L,5,"commit('SET_PERMS', data.perms)"));
		sList.add(new Statement(133000L,5,"commit('SET_NAME', data.name)"));
		sList.add(new Statement(134000L,5,"// commit('SET_AVATAR', data.avatar)"));
		sList.add(new Statement(135000L,5,"// commit('SET_INTRODUCTION', data.introduction)"));
		sList.add(new Statement(136000L,5,"dispatch('GenerateRoutes', data) // 动态修改权限后 重绘侧边菜单"));
		sList.add(new Statement(137000L,5,"resolve()"));
		sList.add(new Statement(138000L,4,"})"));
		sList.add(new Statement(139000L,3,"})"));
		sList.add(new Statement(140000L,2,"}"));
		sList.add(new Statement(141000L,1,"}"));
		sList.add(new Statement(142000L,0,"}"));
		sList.add(new Statement(143000L,0,""));
		sList.add(new Statement(144000L,0,"export default user"));
		return WriteableUtil.merge(sList);
	}

}
